package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/conf"
	"im_sys/apps/im/ws/internal/config"
	"im_sys/apps/im/ws/internal/handler"
	"im_sys/apps/im/ws/internal/svc"
	"im_sys/apps/im/ws/ws_server"
)

var configFile = flag.String("f", "etc/dev/im.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	if err := c.SetUp(); err != nil {
		panic(err)
	}

	ctx := svc.NewServiceContext(c)

	// 将服务地址和要使用的鉴权中间件传给server
	server := ws_server.NewServer(c.ListenOn,
		ws_server.WithServerAuthentication(handler.NewJwtAuth(ctx)),
		ws_server.WithServerAck(ws_server.NoAck),
		//ws_server.WithServerMaxConnectionIdle(5*time.Second),
	) // 之后的参数为定义在options中的更改服务配置的函数
	defer server.Stop()

	// 注册路由
	handler.RegisterHandlers(server, ctx)

	fmt.Println("start websocket server at ", c.ListenOn, "......")
	server.Start()
}
